import { EventEmitter } from 'events'
import { existsSync } from 'fs-extra'
import { PROJECT_ITEM_TYPE } from './enums'
import { HostType } from '../../../constants/common'
import { methodEvaler } from '../../../core/common'

const _evalMethod = methodEvaler(HostType.pr)

export class ProjectItem extends EventEmitter {
  type: PROJECT_ITEM_TYPE
  treePath: string
  timeDisplayFormat: number
  sourceTimebase: string
  interpretedTimebase: string
  nodeId: string
  name: string
  children?: Array<ProjectItem>
  constructor(item: any) {
    super()
    this.type = item.type
    this.treePath = item.treePath
    this.timeDisplayFormat = item.timeDisplayFormat
    this.sourceTimebase = item.sourceTimebase
    this.interpretedTimebase = item.interpretedTimebase
    this.nodeId = item.nodeId
    this.name = item.name
    if (item.children) {
      this.children = item.children.map((i: any) => new ProjectItem(i))
    }
  }
  evalScript(method: string, ...args: any) {
    return _evalMethod(method, this.nodeId, ...args)
  }
  attachProxy(mediaPath: string, isHiRes: 0 | 1) {
    // check mediaPath exists or not
    if (!existsSync(mediaPath)) {
      throw new Error('file not exist: ' + mediaPath)
    }
    return this.evalScript('attachProxy', mediaPath, isHiRes)
  }
  canChangeMediaPath() {
    return this.evalScript('canChangeMediaPath')
  }
  canProxy() {
    return this.evalScript('canProxy')
  }
  changeMediaPath(newPath: string, overrideChecks: boolean) {
    // check mediaPath exists or not
    if (!existsSync(newPath)) {
      throw new Error('file not exist: ' + newPath)
    }
    return this.evalScript('changeMediaPath', newPath, overrideChecks)
  }
  clearOutPoint() {
    return this.evalScript('clearOutPoint')
  }
  createBin(name: string) {
    if (this.type !== PROJECT_ITEM_TYPE.BIN) {
      throw new Error('project item not support')
    }
    return this.evalScript('createBin', name)
  }
  createSmartBin(name: string, queryString: string) {
    if (this.type !== PROJECT_ITEM_TYPE.BIN) {
      throw new Error('project item not support')
    }
    return this.evalScript('createBin', name, queryString)
  }
  createSubClip(
    name: string,
    startTime: string,
    endTime: string,
    hasHardBoundaries: 0 | 1,
    takeAudio: 0 | 1,
    takeVideo: 0 | 1
  ) {
    // 返回的素材要处理成projectItem
    return this.evalScript(
      'createSubClip',
      name,
      startTime,
      endTime,
      hasHardBoundaries,
      takeAudio,
      takeVideo
    )
  }
  deleteBin() {
    if (this.type !== PROJECT_ITEM_TYPE.BIN) {
      throw new Error('project item not support')
    }
    return this.evalScript('deleteBin')
  }
  findItemsMatchingMediaPath(pathToMatch: string, ignoreSubClips: 0 | 1) {
    return this.evalScript(
      'findItemsMatchingMediaPath',
      pathToMatch,
      ignoreSubClips
    )
  }
  refreshMedia() {
    return this.evalScript('refreshMedia')
  }
  getMetadata(key: string) {
    return this.evalScript('getMetadata', key)
  }
  setMetadata(key: string, value: string) {
    return this.evalScript('setMetadata', key, value)
  }
}
